package org.dromara.iot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.iot.domain.IotAnnualPlan;
import org.dromara.iot.domain.bo.IotAnnualPlanBo;
import org.dromara.iot.domain.bo.IotElevatorBo;
import org.dromara.iot.domain.vo.IotAnnualPlanVo;
import org.dromara.iot.domain.vo.IotElevatorVo;
import org.dromara.iot.mapper.IotAnnualPlanMapper;
import org.dromara.iot.service.IIotAnnualPlanService;
import org.dromara.iot.service.IIotElevatorService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 电梯年检计划Service业务层处理
 *
 * @author lsl
 * @date 2024-06-16
 */
@RequiredArgsConstructor
@Service
public class IotAnnualPlanServiceImpl implements IIotAnnualPlanService {

    private final IotAnnualPlanMapper baseMapper;

    private final IIotElevatorService elevatorInfoService;

    /**
     * 查询电梯年检计划
     */
    @Override
    public IotAnnualPlanVo queryById(Long planId) {
        return baseMapper.selectVoById(planId);
    }

    /**
     * 查询电梯年检计划列表
     */
    @Override
    public TableDataInfo<IotAnnualPlanVo> queryPageList(IotAnnualPlanBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IotAnnualPlan> lqw = buildQueryWrapper(bo);
        Page<IotAnnualPlanVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询电梯年检计划列表
     */
    @Override
    public List<IotAnnualPlanVo> queryList(IotAnnualPlanBo bo) {
        LambdaQueryWrapper<IotAnnualPlan> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IotAnnualPlan> buildQueryWrapper(IotAnnualPlanBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IotAnnualPlan> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getElevatorId() != null, IotAnnualPlan::getElevatorId, bo.getElevatorId());
        lqw.like(StringUtils.isNotBlank(bo.getElevatorName()), IotAnnualPlan::getElevatorName, bo.getElevatorName());
        lqw.eq(StringUtils.isNotBlank(bo.getRegisteredCode()), IotAnnualPlan::getRegisteredCode, bo.getRegisteredCode());
        lqw.eq(bo.getAnnualDate() != null, IotAnnualPlan::getAnnualDate, bo.getAnnualDate());
        return lqw;
    }

    /**
     * 新增电梯年检计划
     */
    @Override
    public Boolean insertByBo(IotAnnualPlanBo bo) {
        IotAnnualPlan add = MapstructUtils.convert(bo, IotAnnualPlan.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPlanId(add.getPlanId());
        }
        return flag;
    }

    /**
     * 修改电梯年检计划
     */
    @Override
    public Boolean updateByBo(IotAnnualPlanBo bo) {
        IotAnnualPlan update = MapstructUtils.convert(bo, IotAnnualPlan.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IotAnnualPlan entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除电梯年检计划
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 生成年检计划
     */
    @Override
    public int generate() {
        List<IotElevatorVo> elevatorList = elevatorInfoService.queryList(new IotElevatorBo());
        if (CollUtil.isEmpty(elevatorList)) {
            throw new RuntimeException("没有电梯信息");
        }

        IotAnnualPlan annualPlan;
        for (IotElevatorVo elevatorVo : elevatorList) {
            annualPlan = new IotAnnualPlan();
            annualPlan.setElevatorId(elevatorVo.getElevatorId());
            IotAnnualPlanVo iotAnnualPlanVo = this.selectByElevatorId(elevatorVo.getElevatorId());
            if (ObjectUtil.isNotNull(iotAnnualPlanVo)) {
                BeanUtil.copyProperties(elevatorVo, iotAnnualPlanVo);
                iotAnnualPlanVo.setAnnualDate(elevatorVo.getNextAnnualDate());
                baseMapper.updateById(BeanUtil.toBean(iotAnnualPlanVo, IotAnnualPlan.class));
            } else {
                BeanUtil.copyProperties(elevatorVo, annualPlan);
                //保存下次年检日期作为年检计划的年检日期
                annualPlan.setAnnualDate(elevatorVo.getNextAnnualDate());
                baseMapper.insert(annualPlan);
            }
        }
        return elevatorList.size();
    }

    /**
     * 根据电梯ID查询年检计划
     *
     * @param elevatorId 电梯ID
     * @return 结果
     */
    @Override
    public IotAnnualPlanVo selectByElevatorId(Long elevatorId) {
        LambdaQueryWrapper<IotAnnualPlan> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(IotAnnualPlan::getElevatorId, elevatorId);
        return baseMapper.selectVoOne(wrapper);
    }
}
